package com.czy.seed.mvc.home.controller;

import com.czy.seed.mvc.base.controller.BaseController;
import com.czy.seed.mvc.home.entity.Quotation;
import com.czy.seed.mvc.home.entity.QuoteOrCompare;
import com.czy.seed.mvc.home.service.PurchaseDetailedListService;
import com.czy.seed.mvc.home.service.QuotationService;
import com.czy.seed.mvc.home.service.QuoteOrCompareService;
import com.czy.seed.mvc.util.Res;
import com.czy.seed.mybatis.base.QueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liuyun
 * Created by liuyun  8:57 2017/12/7.
 */


@RestController
@RequestMapping("/home/quotation")
public class QuotationController extends BaseController<Quotation> {

    @Autowired
    private QuotationService quotationService;

    @Autowired
    private QuoteOrCompareService quoteOrCompareService;

    public static Logger logger = LoggerFactory.getLogger(QuotationController.class);


    /**
     * 根据询价单ID来查询报价单信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectByQuoteOrCompareId")
    public Res selectByQuoteOrCompareId(Long id) {
        QueryParams queryParams = new QueryParams(Quotation.class);
        QueryParams.Criteria criteria = queryParams.createCriteria();
        criteria.andEqualTo("quoteOrCompareId", id);
        criteria.andIsNotNull("quoteSum");
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try {
            List<Quotation> QuotationList = quotationService.selectListByParams(queryParams);
            pageInfo.put("QuotationList", QuotationList);
        } catch (Exception e) {

            logger.error("根据询价单ID来查询报价单信息出错：{}", e);
        }

        return Res.ok(pageInfo);
    }
    /**
     * 根据报价单ID来查询报价单信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectQuoteById")
    public Res selectQuoteById(Long id) {
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try {

            Quotation QuotationList = quotationService.selectByPrimaryKey(id);
            pageInfo.put("QuotationList", QuotationList);
        } catch (Exception e) {

            logger.error("根据ID来查询报价单信息出错：{}", e);
        }

        return Res.ok(pageInfo);
    }

    /**
     * 根据采购清单ID来查询报价单信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectListQuoteById")
    public Res selectListQuoteById(Long id) {
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try {
            QueryParams queryParams = new QueryParams(QuoteOrCompare.class);
            QueryParams.Criteria criteria = queryParams.createCriteria();
            criteria.andEqualTo("purchaseId", id);
            /*criteria.andCondition("quotationList.type = 8 ");*/
            List<QuoteOrCompare> quoteOrCompareList = quoteOrCompareService.selectListRelativeByParams(queryParams);
            if(quoteOrCompareList.size()>0 ){
                pageInfo.put("QuotationList", quoteOrCompareList.get(0).getQuotationList());
            }

        } catch (Exception e) {

            logger.error("根据ID来查询报价单信息出错：{}", e);
        }

        return Res.ok(pageInfo);
    }
    /**
     * 添加报价单信息
     *
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping("/addQuotation")
    public Res addQuotation(HttpServletRequest request) {
        String ord = request.getParameter("quotation");
        String msg = "";
        try {
            msg = quotationService.addQuotation_impl(request);

        } catch (Exception e) {
            msg = e.getMessage();
            logger.error("添加报价单信息出错：{}", e);
        }

        return Res.ok(msg);

    }

    /**
     * 根据报价单信息，设置选中单状态及其其他子单状态，设置主单状态
     * 设置当前单选中状态（8），将其他姊妹单设置为报价结束（1）
     * 设置主单状态  询价结束（1）
     *
     * @param id
     * @return
     */
    @RequestMapping("/updateTypeByQuotationId")
    public Res updateTypeByQuotationId(Long id, int ifshow) {
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try {
            pageInfo= quotationService.updateTypeByQuotationId_impl(id,ifshow);
        } catch (Exception e) {
            pageInfo.put("message",e.getMessage());
            logger.error("依据报价单id更新姊妹单和主询价单状态失败：{}", e);
        }
        return Res.ok(pageInfo);
    }
}
